package me.sdevil507.systemAssist.systemUserWorkSapce.service.impl;

import com.google.common.collect.Lists;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.extern.slf4j.Slf4j;
import me.sdevil507.org.po.OrgUserPo;
import me.sdevil507.org.service.OrgRoleService;
import me.sdevil507.org.service.OrgUserService;
import me.sdevil507.supports.jpa.PageParamsDTO;
import me.sdevil507.supports.jpa.SortParamsDTO;
import me.sdevil507.supports.shiro.helper.OrgAccountHelper;
import me.sdevil507.systemAssist.systemLog.po.QSystemAccessLogPo;
import me.sdevil507.systemAssist.systemUserWorkSapce.dto.SystemUserWorkSpaceDTO;
import me.sdevil507.systemAssist.systemUserWorkSapce.mapper.SystemUserWorkSpaceMapper;
import me.sdevil507.systemAssist.systemUserWorkSapce.po.QSystemUserWorkSpacePO;
import me.sdevil507.systemAssist.systemUserWorkSapce.po.SystemUserWorkSpacePO;
import me.sdevil507.systemAssist.systemUserWorkSapce.repository.SystemUserWorkSpaceRepository;
import me.sdevil507.systemAssist.systemUserWorkSapce.service.SystemUserWorkSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: tips_back_end
 * @description:
 * @author: kzl
 * @create: 2020-07-02 13:45
 **/
@Service
@Slf4j
public class SystemUserWorkSpaceServiceImpl implements SystemUserWorkSpaceService {
    @Resource
    private SystemUserWorkSpaceRepository systemUserWorkSpaceRepository;
    @Autowired
    private SystemUserWorkSpaceMapper systemUserWorkSpaceMapper;
    @Autowired
    private OrgUserService orgUserService;
    @Autowired
    private OrgRoleService orgRoleService;

    @PersistenceContext
    private EntityManager entityManager;

    @Autowired
    JPAQueryFactory jpaQueryFactory;

    @Override
    public Page<SystemUserWorkSpacePO> readAllByPage(SystemUserWorkSpaceDTO searchDTO, PageParamsDTO pageParamsDto, SortParamsDTO sortParamsDto) {

        QSystemUserWorkSpacePO qSystemUserWorkSpacePO = QSystemUserWorkSpacePO.systemUserWorkSpacePO;

        // 开启谓语查询条件
        Predicate predicate = this.getPredicate(qSystemUserWorkSpacePO, searchDTO);

        Sort sort;
        if (null == sortParamsDto) {
            sort = Sort.by(Sort.Direction.DESC, "createTime");
        } else {
            // 排序
            sort = sortParamsDto.getSort();
            if (null == sort) {
                sort = Sort.by(Sort.Direction.DESC, "createTime");
            }
        }

        // 执行返回
        Pageable pageable = pageParamsDto.getPageableWithSort(sort);

        return systemUserWorkSpaceRepository.findAll(predicate, pageable);

    }

    private  Predicate getPredicate(QSystemUserWorkSpacePO qSystemUserWorkSpacePO,  SystemUserWorkSpaceDTO searchDTO){

//        // 开启谓语查询条件
        Predicate predicate = qSystemUserWorkSpacePO.id.isNotNull();

        Long moduleId = searchDTO.getModuleId();
        if (!StringUtils.isEmpty(moduleId)) {
            predicate = ExpressionUtils.and(predicate, qSystemUserWorkSpacePO.moduleId.eq(moduleId));
        }

        String modulePath = searchDTO.getModulePath();
        if (!StringUtils.isEmpty(modulePath)) {
            predicate = ExpressionUtils.and(predicate, qSystemUserWorkSpacePO.modulePath.eq(modulePath));
        }


        Boolean isDel = searchDTO.getIsDel();
        predicate = ExpressionUtils.and(predicate, qSystemUserWorkSpacePO.isDel.eq(isDel));

        return predicate;
    }

    @Override
    public void create(SystemUserWorkSpaceDTO systemUserWorkSpaceDTO) {
        systemUserWorkSpaceDTO.setUserId(OrgAccountHelper.getUserId());
        systemUserWorkSpaceRepository.save(systemUserWorkSpaceMapper.dto2po(systemUserWorkSpaceDTO));
    }

    @Override
    public List<SystemUserWorkSpacePO> readAll(SystemUserWorkSpaceDTO systemUserWorkSpaceDTO) {
        QSystemUserWorkSpacePO qSystemUserWorkSpacePO = QSystemUserWorkSpacePO.systemUserWorkSpacePO;
        return jpaQueryFactory.selectFrom(qSystemUserWorkSpacePO)
                .where(qSystemUserWorkSpacePO.userId.eq(systemUserWorkSpaceDTO.getUserId()))
                .fetch();
    }

    @Override
    public void delete(Long id) {
//        SystemUserWorkSpacePO systemUserWorkSpacePO = systemUserWorkSpaceRepository.findById(id).get();
//        //逻辑删除，将删除状态改为true
//        systemUserWorkSpacePO.setIsDel(true);
        systemUserWorkSpaceRepository.logicDelete(id, OrgAccountHelper.getUserId());
    }

    @Override
    public void clear(Long id) {
        systemUserWorkSpaceRepository.deleteById(id);
    }


}
